package com.base.myproject.client.HR.attendance;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.GroupSummaryView;
import com.extjs.gxt.ui.client.widget.grid.SummaryColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.SummaryType;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author cxghiu410@gmail.com 
 * 完成员工用餐的查询，查询条件可能还得修改
 */
public class SelectEmployeesEat extends BaseJDBCPanel {
	DateField seach_by_start_date = new DateField();// 查询开始日期
	DateField seach_by_end_date = new DateField();// 查询结束日期
	Button seach_bt = new Button("查询");// 查询按钮
	
	public SelectEmployeesEat() {
		init();
	}

	public void init() {
		initWeigetx();
	}
	
	public void complete() {
		super.complete();
	}

	public void initWeigetx() {
		store = new BaseStore();
		
		seach_by_start_date.setPropertyEditor(new DateTimePropertyEditor(
				"yyyy-MM-dd"));
		seach_by_end_date.setPropertyEditor(new DateTimePropertyEditor(
				"yyyy-MM-dd"));
		seach_bt.addListener(Events.OnClick, getListener());
		
		HorizontalPanel form = new HorizontalPanel();

		form.add(new Label("  日期查询从："));
		form.add(seach_by_start_date);

		form.add(new Label("  到："));
		form.add(seach_by_end_date);

		form.add(seach_bt);
		// 垂直布局
		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(10);
		vp.add(form);
		vp.add(getBaseGrid());
		add(vp);
	}

	// 创建数据表
	BaseStore store = new BaseStore();
	// 创建一个grid
	BaseGrid<BusiObject> grid = null;

	BaseGrid<BusiObject> getBaseGrid() {
		if (grid == null) {
			createGrid();
		}
		return grid;
	}

	void createGrid() {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();

		ColumnConfig C1 = new ColumnConfig("date", "日期", 100);
		columns.add(C1);

		ColumnConfig C2 = new ColumnConfig("zao", "早餐", 80);
		columns.add(C2);

		ColumnConfig C3 = new ColumnConfig(
				"zhong", "中餐", 80);
		columns.add(C3);

		ColumnConfig C4 = new ColumnConfig("wan", "晚餐", 80);
		columns.add(C4);

		ColumnConfig C6 = new ColumnConfig("ye", "宵夜", 80);
		columns.add(C6);

		ColumnConfig C7 = new ColumnConfig("sum", "每日统计", 80);
		columns.add(C7);

		ColumnModel cm = new ColumnModel(columns);

		grid = new BaseGrid<BusiObject>(store, cm, false, false);
		grid.setTitle("查询结果");
		Tools.setResize(grid, 0.99, 0.85);
	}

	public void to_seach() {
		final String start_date = seach_by_start_date.getRawValue();
		final String end_date = seach_by_end_date.getRawValue();
		String sql = "select name,bgnTime,endTime from hr_Attendance_eatTime where isUse = '有效'";

		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getDataSet(sql, new AsyncCallback<DataSet>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(DataSet result) {
				String sql1 = "select c.da,c.zhong"; // ,isnull(b.zao,0)
														// zao,c.zhong,isnull(a.wan,0)
														// wan,(c.zhong+isnull(b.zao,0)+isnull(a.wan,0))
														// sum from
				String sql2 = ""; // 定义其它餐的字符串
				String sql3 = ""; // 定义中餐的字符串
				String sql4 = ",(c.zhong"; // 定义sum的字符串
				store.removeAll();
				if (result.getRowCount() > 0) {// 返回结果为0
					for (int i = 0; i < result.getRowCount(); i++) {
						if (result.getValue(i, "name").equals("中餐")) {
							sql3 = sql3
									+ " (select SubString(convert(varchar(20),checktime,120),0,11) da,count(*) "
									+ "zhong from attendance_view where touse='饭堂' and "
									+ "SubString(convert(varchar(20),checktime,120),12,5) between '"
									+ result.getValue(i, "bgnTime")
									+ "' and '"
									+ result.getValue(i, "endTime")
									+ "' group by "
									+ "SubString(convert(varchar(20),checktime,120),0,11)) as c left join";
						} else if (result.getValue(i, "name").equals("早餐")) {
							sql1 = sql1 + ",isnull(b.zao,0) zao";
							sql4 = sql4 + "+isnull(b.zao,0)";
							sql2 = sql2
									+ " (select SubString(convert(varchar(20),checktime,120),0,11) da,count(*) "
									+ "zao from attendance_view where touse='饭堂' and "
									+ "SubString(convert(varchar(20),checktime,120),12,5) between '"
									+ result.getValue(i, "bgnTime")
									+ "' and '"
									+ result.getValue(i, "endTime")
									+ "' group by "
									+ "SubString(convert(varchar(20),checktime,120),0,11)) as b on b.da = c.da left join";
						} else if (result.getValue(i, "name").equals("晚餐")) {
							sql1 = sql1 + ",isnull(a.wan,0) wan";
							sql4 = sql4 + "+isnull(a.wan,0)";
							sql2 = sql2
									+ " (select SubString(convert(varchar(20),checktime,120),0,11) da,count(*) "
									+ "wan from attendance_view where touse='饭堂' and "
									+ "SubString(convert(varchar(20),checktime,120),12,5) between '"
									+ result.getValue(i, "bgnTime")
									+ "' and '"
									+ result.getValue(i, "endTime")
									+ "' group by "
									+ "SubString(convert(varchar(20),checktime,120),0,11)) as a on a.da = c.da left join";
						} else {
							sql1 = sql1 + ",isnull(d.ye,0) ye";
							sql4 = sql4 + "+isnull(d.ye,0)";
							sql2 = sql2
									+ " (select SubString(convert(varchar(20),checktime,120),0,11) da,count(*) "
									+ "ye from attendance_view where touse='饭堂' and "
									+ "SubString(convert(varchar(20),checktime,120),12,5) between '"
									+ result.getValue(i, "bgnTime")
									+ "' and '"
									+ result.getValue(i, "endTime")
									+ "' group by "
									+ "SubString(convert(varchar(20),checktime,120),0,11)) as d on d.da = c.da left join";
						}
					}
					sql2 = sql2.substring(0, sql2.length() - 10);
					sql1 = sql1 + sql4 + ") sum from" + sql3 + sql2
							+ " where c.da between '" + start_date + "' and '"
							+ end_date + "'";
				}
				System.out.println("************************" + sql1);
				final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
						.create(com.base.myproject.client.tools.GreetingService.class);
				greetingService.getDataSet(sql1, new AsyncCallback<DataSet>() {
					@Override
					public void onFailure(Throwable caught) {
					}

					@Override
					public void onSuccess(DataSet result) {
						store.removeAll();
						BaseModelData model = null;
						int zaoSum = 0;
						int zhongSum = 0;
						int wanSum = 0;
						int yeSum = 0;
						if (result.getRowCount() > 0) {// 返回结果为0
							for (int i = 0; i < result.getRowCount(); i++) {
								String zao = result.getValue(i, "zao") == null ? "0"
										: result.getValue(i, "zao");
								String zhong = result.getValue(i,
								"zhong") == null ? "0" : result
										.getValue(i, "zhong");
								String wan = result.getValue(i, "wan") == null ? "0"
										: result.getValue(i, "wan");
								String ye = result.getValue(i, "ye") == null ? "0"
										: result.getValue(i, "ye");
								
								zaoSum += Integer.valueOf(zao);
								zhongSum += Integer.valueOf(zhong);
								wanSum += Integer.valueOf(wan);
								yeSum += Integer.valueOf(ye);

								model = new BaseModelData();

								model.set("date", result.getValue(i, "da"));
								model.set("zao",zao);
								model.set("zhong", zhong);
								model.set("wan",wan);
								model.set("ye",ye);
								model.set("sum", result.getValue(i, "sum"));

								store.add(model, false, false);
							}
							model = new BaseModelData();
							
							model.set("date", "总结");
							model.set("zao",zaoSum);
							model.set("zhong", zhongSum);
							model.set("wan",wanSum);
							model.set("ye",yeSum);
							model.set("sum", "");

							store.add(model, false, false);

							grid.setStore(store);
							grid.getView().refresh(true);
						}
					}
				});
			}
		});
	}

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}
		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {

						} else if (be.getType() == Events.OnClick) {
							if (be.getSource().equals(seach_bt)) {
								if (seach_by_start_date.getRawValue()
										.equals("")
										|| seach_by_end_date.getRawValue()
												.equals("")) {
									MessageBox.alert("警告", "请选择查询条件,日期范围必须完整",
											null);
								} else {
									to_seach();
								}

							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}
}
